package model;

import connection.DbConnection;
import bean.PollUser;
import java.sql.*;

public class PollUserImpl {
	
	private static PreparedStatement st;
	private static Connection conn;
	private static ResultSet rs;
	private static PollUser user;
	
	public PollUserImpl(PollUser u) {
		user = u;
		if(checkuser(user)){
			setUserAttributes(user);
		}
	}
	
	public PollUser getUser(){
		return user;
	}
	
	/**
	 * Checks for the given username and password in the database
	 * 
	 * Returns: True if username and password correct, else false.
	 */
	public static boolean checkuser(PollUser user) {
		if (user.getUsername() != null && user.getPassword()!= null) {
			try {
				ResultSet resultSet = null;
				DbConnection conn = new DbConnection();
				resultSet = conn.doSelect("SELECT * FROM PollUser WHERE Username='"+user.getUsername()+"' AND Password='"+user.getPassword()+"'");
				if (resultSet.next()) {
					return true;
				}
			}
			catch (NullPointerException ne) {
				ne.printStackTrace();
				System.out.println("******** NOTICE: Consider looking over DbConnection and ensuring your");
				System.out.println("******** connection details are correct.");
			}
			catch (SQLException sqle) {
				sqle.printStackTrace();
				System.out.println("******** NOTICE: Ensure you have commited data to the database");
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	/**
	 * Method used to check if username is in database.
	 * @param user is PollUser bean
	 * @return true if username is in database
	 */
	public static boolean checkUserNameOnly(PollUser user) {
		
		if (user.getUsername() == null) return false;
		try {
			ResultSet resultSet = null;
			DbConnection conn = new DbConnection();
			resultSet = conn.doSelect("SELECT * FROM PollUser WHERE Username='"+user.getUsername()+"'");
			if (resultSet.next()) {
				return true;
			}
		}
		catch (NullPointerException ne) {
			ne.printStackTrace();
			System.out.println("******** NOTICE: Consider looking over DbConnection and ensuring your");
			System.out.println("******** connection details are correct.");
		}
		catch (SQLException sqle) {
			sqle.printStackTrace();
			System.out.println("******** NOTICE: Ensure you have commited data to the database");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * Sets all PollUser javabean attributes from database (even if empty in database).
	 * 
	 * @param user is PollUser bean
	 */
	public static PollUser setUserAttributes (PollUser user) {
		System.out.println("SUA");
		try {
			ResultSet resultSet = null;
			DbConnection conn = new DbConnection();
			resultSet = conn.doSelect("SELECT * FROM PollUser where Username = '" + user.getUsername() + "'");
			resultSet.next();
			
			user.setPassword(resultSet.getString("Password"));
			user.setEmail(resultSet.getString("Email"));
			System.out.println("rs:"+resultSet.getInt("Accessibility"));
			user.setAccessibility(resultSet.getInt("Accessibility"));
			user.setFirstname(resultSet.getString("FName"));
			user.setLastname(resultSet.getString("LName"));
			user.setCreateDate(resultSet.getDate("CreateTime"));
			user.setExpireDate(resultSet.getDate("ExpireDate"));
			user.setGenericUser(resultSet.getInt("GenericUser"));
		} catch (NullPointerException npe) {
			npe.printStackTrace();
			System.out.println("******** Null Pointer Exception when executing setUserAttributes method.");
			System.out.println("******** Ie. Your user bean hasn't been created!");
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			System.out.println("******** SQL problem when executing setUserAttribute method.");
			System.out.println("******** Ie. Your sql query is wrong!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("ret");
		return user;
	}
	/**
	 * Adds pollUser bean to database
	 */
	public void addUser(PollUser user){
		
		try{
			rs = null;
			conn = new DbConnection().getConn();
			
			System.out.println("*****//RESULTSET NEXT");
			st = conn.prepareStatement("insert into PollUser (username, password, email, accessibility) values (?,?,?,?)");
			
			st.setString(1, user.getUsername());
			st.setString(2, user.getPassword());
			st.setString(3, user.getEmail());
			st.setInt(4, user.getAccessibility());
			rs=st.executeQuery();
	    	
		}catch(Exception e){
			System.out.println("*************Error in adduser method of PollUserImpl");
			e.printStackTrace();
		}
		System.out.println("----- Success for addUser method of PollUserImpl");
	}
}
